<!doctype html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>自定义桌面</title>
	<link rel="stylesheet" href="../css/bootstrap.min.css">
    <link href="../css/font-awesome.min.css" rel="stylesheet">
    <link rel="stylesheet" href="dist/gridstack.css"/>
	<link rel="stylesheet" type="text/css" href="css/default.css">
	<style type="text/css">
        .grid-stack {
            background: #f6f6f6;
        }

        .grid-stack-item-content {
            color: #2c3e50;
            text-align: center;
            background-color: #ffffff;
        }
        .widget_resizable_helper{
            border:3px solid #A29B9B;
            z-index:999999 !important;
        }
    </style>
</head>
<body>
	<div class="htmleaf-container">
		<div class="htmleaf-content bgcolor-3">
			<div class="container-fluid">
                <div>
                    <button data-bind="click: add_new_widget">添加模块</button>
                </div>

                <br>

                <div data-bind="component: {name: 'dashboard-grid', params: $data}"></div>
		    </div>
	    </div>
	</div>
	
	<script src="../js/jquery.min.js" type="text/javascript"></script>
	<script src="../js/jquery.ba-resize.min.js" type="text/javascript"></script>
    <script src="../js/jquery-ui.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    <script src="js/lodash.min.js"></script>
    <script src="js/knockout-min.js"></script>
    <script src="dist/gridstack.js"></script>
    <script type="text/javascript">
        ko.components.register('dashboard-grid', {
            viewModel: {
                createViewModel: function (controller, componentInfo) {
                    var ViewModel = function (controller, componentInfo) {
                        var grid = null;

                        this.widgets = controller.widgets;

                        this.afterAddWidget = function (items) {
                            if (grid == null) {
                                grid = $(componentInfo.element).find('.grid-stack').gridstack({
                                    auto: false
                                }).data('gridstack');
                            }

                            var item = _.find(items, function (i) { return i.nodeType == 1 });
                            grid.add_widget(item);
                            ko.utils.domNodeDisposal.addDisposeCallback(item, function () {
                                grid.remove_widget(item);
                            });
                        };
                    };

                    return new ViewModel(controller, componentInfo);
                }
            },
            template:
                [
                    '<div class="grid-stack" data-bind="foreach: {data: widgets, afterRender: afterAddWidget}">',
                    '   <div class="grid-stack-item" data-bind="attr: {\'data-id\':$data.id,\'data-content-src\':$data.src,\'data-gs-x\': $data.x, \'data-gs-y\': $data.y, \'data-gs-width\': $data.width, \'data-gs-height\': $data.height, \'data-gs-auto-position\': $data.auto_position}">',
                    '       <div  class="grid-stack-item-content">',
                    '   </div>',
                    '       <div class="ui-delable-handle ui-delable-se" style="z-index: 90;" data-bind="click: $root.delete_widget">x</div>',
                    '   </div>',
                    '</div> '
                ].join('')
        });

        $(function () {
            var Controller = function (widgets) {
                var self = this;

                this.widgets = ko.observableArray(widgets);

                this.add_new_widget = function () {
                    let n = {
                        id:Math.floor(1 + 3 * Math.random()),
                        x: 0,
                        y: 0,
                        width: 4,
                        height: 3,
                        src:'../test/page-6.html',
                        auto_position: true
                    }
                    this.widgets.push(n);
                    // TODO 添加模块
                    console.log('add:')
                    console.log({id:n.id,x: n.x, y: n.y, width: n.width, height: n.height,src:n.src})
                };

                // TODO 删除模块
                this.delete_widget = function (item) {
                    self.widgets.remove(item);
                };


            };

            var widgets = [
                {id:101,x: 2, y: 2, width: 4, height: 2,src:'../test/page-1.html'},
                {id:102,x: 2, y: 0, width: 4, height: 2,src:'../test/page-2.html'},
                {id:103,x: 6, y: 0, width: 6, height: 4,src:'../test/page-3.html'},
                {id:104,x: 0, y: 0, width: 2, height: 4,src:'../test/page-4.html'}
            ];

            var widgets2 = [
                {id:101,x: 0, y: 0, width: 2, height: 2,src:'../images/desktop/01.jpg'},
                {id:102,x: 2, y: 0, width: 4, height: 2,src:'../images/desktop/02.jpg'},
                {id:103,x: 6, y: 0, width: 2, height: 4,src:'../images/desktop/03.jpg'},
                {id:104,x: 0, y: 2, width: 4, height: 2,src:'../images/desktop/04.jpg'}
            ];

            var controller = new Controller(widgets);
            ko.applyBindings(controller);
        });
    </script>
</body>
</html>